package com.cndw.kungfu.domain;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.battle.Team;
import com.cndw.kungfu.model.scene.SubLineCore;
import com.cndw.kungfu.vo.SceneVO;

/**
 * 场景实体类
 * 
 * @author fantadust
 * @date 2011-4-29 下午04:58:20
 */
public class Scene extends SceneBase {

	/**
	 * 场景的用户id
	 */
	private ConcurrentHashMap<Long, Boolean> playerIds = new ConcurrentHashMap<Long, Boolean>(32, 0.75f, 16);
	/**
	 * 场景的二线城市
	 */
	private List<Integer> undercityIdsList;
	/**
	 * 分线的用户列表
	 */
	private Map<Integer, ConcurrentHashMap<Long, Boolean>> subLinePlayerIdsMap;
	/**
	 * 分线上用户个数的数组
	 */
	private SubLineCore subLineCore;

	private SceneVO sceneVO;

	private int[] birthPoint = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };

	public int[] getBirthPoint() {
		return birthPoint;
	}

	private ConcurrentHashMap<Long, Boolean> getSubLineIds(int subLine) {
		if (subLine == 0) {
			return playerIds;
		}
		if (subLinePlayerIdsMap == null) {
			synchronized (this) {
				if (subLinePlayerIdsMap == null) {
					subLinePlayerIdsMap = new ConcurrentHashMap<Integer, ConcurrentHashMap<Long, Boolean>>();
				}

			}
		}
		if (subLinePlayerIdsMap.containsKey(subLine) == false) {
			synchronized (this) {
				if (subLinePlayerIdsMap.containsKey(subLine) == false) {
					subLinePlayerIdsMap.put(subLine, new ConcurrentHashMap<Long, Boolean>());
				}
			}
		}
		return subLinePlayerIdsMap.get(subLine);
	}

	public boolean openSubLine() {
		return isCityMain() || isCityField();
	}

	public int makeSubLine(Player player) {
		int subLine = player.getSubLine();
		if (ConstantsConfig.getSysSubLineNum() > 0 && openSubLine()) {
			if (subLineCore == null) {
				synchronized (this) {
					if (subLineCore == null) {
						subLineCore = new SubLineCore(ConstantsConfig.getSysSubLineNum());
					}
				}
			}
			if (subLine > ConstantsConfig.getSysSubLineNum()) {
				subLine = 0;
			}

			if (subLine == 0) {
				subLine = subLineCore.makeSubLine();
				player.setSubLine(subLine);
			} else {
				subLineCore.enterSubLine(subLine);
			}
			getSubLineIds(subLine).put(player.getPlayerId(), true);

		} else if (player.getSubLine() > 0) { // 服务器取消了分线
			player.setSubLine(0);
			subLine = 0;
		}
		playerIds.put(player.getPlayerId(), true);

		return subLine;
	}

	public void removePlayerId(long playerId, int subLine) {
		if (playerIds.containsKey(playerId)) {
			playerIds.remove(playerId);
		}
		if (subLine > 0) {
			if (subLineCore != null) {
				subLineCore.exitSubLine(subLine);
			}

			if (subLinePlayerIdsMap != null && subLinePlayerIdsMap.containsKey(subLine)) {
				getSubLineIds(subLine).remove(playerId);
			}
		}
	}

	public Set<Long> getPlayerIds(int subLine) {
		return getSubLineIds(subLine).keySet();
	}

	public boolean isCityMain() {
		return type == Constants.CITY_MAIN;
	}

	public boolean isCityField() {
		return type == Constants.CITY_FIELD;
	}

	public boolean isCityUndercity() {
		return type == Constants.CITY_UNDER;
	}
	/**
	 * 是否是特殊加载场景,先暂时这么写
	 * @return
	 */
	public boolean isSpecialBroadcast(){
		return id == 66005;
	}

	/**
	 * 是否塞外
	 * 
	 * @return
	 */
	public boolean isCityWall() {
		return type == Constants.CITY_WALL;
	}
	/**
	 * 是否是秘境
	 * @return
	 */
	public boolean isCityUnionWar(){
		return type == Constants.CITY_UNION;
	}

	public boolean isCityFam(){
		return type == Constants.CITY_FAM;
	}	
	/**
	 * 是否围城
	 * 
	 * @return
	 */
	public boolean isSiegeWar() {
		return rootId == Constants.siegeWarRootId;
	}

	public List<Integer> getUndercityIdsList() {
		return undercityIdsList;
	}

	public void setUndercityIdsList(List<Integer> undercityIdsList) {
		this.undercityIdsList = undercityIdsList;
	}

	/**
	 * 场景广播
	 * 
	 * @param player
	 * @param data
	 * @param actionName
	 */
	public void broadcast(Player player, Object data, String actionName) {
		if (isCityUndercity()) {
			Team team = player.getTeam();
			if (team != null) {
				team.broadcast(data, actionName, player.getSceneId());
			}
		}else if (isSpecialBroadcast()) {
			Collection<Long> ids = Context.getSceneServer().getOnSceneIds(player, this);
			if (ids != null && !ids.isEmpty()) {
				Context.getTcpServer().broadcast(data, actionName, ids);
			}
		} else {
			String idKey = Context.getSceneServer().getRadioKey(id, player.getSubLine());
			Context.getTcpServer().broadcast(data, actionName, idKey);
		}
	}

	public SceneVO toSceneVO() {
		if (sceneVO == null) {// 不考虑并发情况
			sceneVO = new SceneVO(this);
		}
		return sceneVO;
	}

	public int[] getSubLineNums() {
		return subLineCore.getSubLineNums();
	}

}
